﻿namespace Hotel.ModulesTest
{
    using ResourcesModule = Hotel.Module.Resources;
    using Hotel.Module.Resources.Models;
    using System.Collections.Generic;
    using Hotel.Module.Management;
    using Hotel.Module.Management.Models;
    using System;
    using Hotel.Web.Controllers;
    using NUnit.Framework;
    using System.Linq;
    using Hotel.Module.Resources;

    [TestFixture]
    public class FreeRoomsByOrderIDTestClass
    {
        [Test]
        public void FreeRoomsByOrderIDTest()
        {
            using (ManagementController m_ManagementController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(IManagement)) as ManagementController)
            {
                using (HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(IResourseManagment)) as HotelResourcesController)
                {
                    List<Order> _orderToDelete = new List<Order>();

                    foreach (var order in m_ManagementController.GetOrders())
                    {
                        if (order.FName == "FreeRoomsByOrderIDTest" && order.LName == "FreeRoomsByOrderIDTest")
                        {
                            _orderToDelete.Add(order);
                        }
                    }
                    foreach (var order in _orderToDelete)
                    {
                        m_ManagementController.Storage.Delete(order);
                    }

                    m_ManagementController.SaveChanges();

                    //

                    Hotel.Web.Models.RoomOrder _order = new Hotel.Web.Models.RoomOrder();
                    _order.ContactInformation = new Hotel.Web.Models.ContactInfo();
                    _order.ContactInformation.Contacts = new Hotel.Web.Models.Contacts();

                    _order.InDate = new DateTime(2013, 10, 25);
                    _order.OutDate = new DateTime(2013, 11, 25);
                    _order.NumberOfPeople = 1;
                    _order.RoomNum = 1;
                    _order.RoomType = "Standard";

                    _order.ContactInformation.Contacts.Phone = "111444";
                    _order.ContactInformation.Contacts.EMail = "abcdef@abcd.abc";
                    _order.ContactInformation.FName = "FreeRoomsByOrderIDTest";
                    _order.ContactInformation.LName = "FreeRoomsByOrderIDTest";

                    Order ModelOrder = new Order();

                    OrderRoomController MVCController = new OrderRoomController();
                    MVCController.CreateOrder(_order);

                    int _id = 0;

                    _id = m_ManagementController.Storage.Find(new Predicate<Order>(p => p.FName == "FreeRoomsByOrderIDTest")).ID;

                    List<Room> listOfRooms = new List<Room>();

                    foreach (var m_order in m_ManagementController.GetOrders())
                    {
                        if (m_order.ID == _id)
                        {
                            foreach (var roomNumber in m_order.RoomPackage)
                            {
                                Room _room = m_ResourcesController.Storage.Find(new Predicate<Room>(p => p.Number == roomNumber));
                                listOfRooms.Add(_room);
                            }
                        }
                    }

                    int init = 0;

                    foreach (var room in listOfRooms)
                    {
                        init = room.Periods.Count;
                    }

                    m_ResourcesController.FreeRoomsByOrderID(_id);

                    foreach (var room in listOfRooms)
                    {
                        Assert.AreEqual(init, room.Periods.Count);
                    }
                }
            }
        }

    }
}
